<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>asserts_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_asserts_asserts_test.js.html">asserts_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.assertsTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.assertsTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line19"></a>goog.require(&#39;goog.asserts.AssertionError&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom&#39;);
<a name="line21"></a>goog.require(&#39;goog.string&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>
<a name="line24"></a>function doTestMessage(failFunc, expectedMsg) {
<a name="line25"></a>  var error = assertThrows(&#39;failFunc should throw.&#39;, failFunc);
<a name="line26"></a>  // Test error message.
<a name="line27"></a>  // Opera 10 adds cruft to the end of the message, so do a startsWith check.
<a name="line28"></a>  assertTrue(&#39;Message check failed. Expected: &#39; + expectedMsg + &#39; Actual: &#39; +
<a name="line29"></a>      error.message, goog.string.startsWith(error.message, expectedMsg));
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function testAssert() {
<a name="line33"></a>  // None of them may throw exception
<a name="line34"></a>  goog.asserts.assert(true);
<a name="line35"></a>  goog.asserts.assert(1);
<a name="line36"></a>  goog.asserts.assert([]);
<a name="line37"></a>  goog.asserts.assert({});
<a name="line38"></a>
<a name="line39"></a>  assertThrows(&#39;assert(false)&#39;, goog.partial(goog.asserts.assert, false));
<a name="line40"></a>  assertThrows(&#39;assert(0)&#39;, goog.partial(goog.asserts.assert, 0));
<a name="line41"></a>  assertThrows(&#39;assert(null)&#39;, goog.partial(goog.asserts.assert, null));
<a name="line42"></a>  assertThrows(&#39;assert(undefined)&#39;,
<a name="line43"></a>               goog.partial(goog.asserts.assert, undefined));
<a name="line44"></a>
<a name="line45"></a>  // Test error messages.
<a name="line46"></a>  doTestMessage(goog.partial(goog.asserts.assert, false), &#39;Assertion failed&#39;);
<a name="line47"></a>  doTestMessage(goog.partial(goog.asserts.assert, false, &#39;ouch %s&#39;, 1),
<a name="line48"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>function testFail() {
<a name="line53"></a>  assertThrows(&#39;fail()&#39;, goog.asserts.fail);
<a name="line54"></a>  // Test error messages.
<a name="line55"></a>  doTestMessage(goog.partial(goog.asserts.fail, false), &#39;Failure&#39;);
<a name="line56"></a>  doTestMessage(goog.partial(goog.asserts.fail, &#39;ouch %s&#39;, 1),
<a name="line57"></a>                &#39;Failure: ouch 1&#39;);
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>function testNumber() {
<a name="line61"></a>  goog.asserts.assertNumber(1);
<a name="line62"></a>  assertThrows(&#39;assertNumber(null)&#39;,
<a name="line63"></a>      goog.partial(goog.asserts.assertNumber, null));
<a name="line64"></a>  // Test error messages.
<a name="line65"></a>  doTestMessage(goog.partial(goog.asserts.assertNumber, null),
<a name="line66"></a>                &#39;Assertion failed: Expected number but got null: null.&#39;);
<a name="line67"></a>  doTestMessage(goog.partial(goog.asserts.assertNumber, &#39;1234&#39;),
<a name="line68"></a>                &#39;Assertion failed: Expected number but got string: 1234.&#39;);
<a name="line69"></a>  doTestMessage(goog.partial(goog.asserts.assertNumber, null, &#39;ouch %s&#39;, 1),
<a name="line70"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testString() {
<a name="line74"></a>  assertEquals(&#39;1&#39;, goog.asserts.assertString(&#39;1&#39;));
<a name="line75"></a>  assertThrows(&#39;assertString(null)&#39;,
<a name="line76"></a>      goog.partial(goog.asserts.assertString, null));
<a name="line77"></a>  // Test error messages.
<a name="line78"></a>  doTestMessage(goog.partial(goog.asserts.assertString, null),
<a name="line79"></a>                &#39;Assertion failed: Expected string but got null: null.&#39;);
<a name="line80"></a>  doTestMessage(goog.partial(goog.asserts.assertString, 1234),
<a name="line81"></a>                &#39;Assertion failed: Expected string but got number: 1234.&#39;);
<a name="line82"></a>  doTestMessage(goog.partial(goog.asserts.assertString, null, &#39;ouch %s&#39;, 1),
<a name="line83"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line84"></a>}
<a name="line85"></a>
<a name="line86"></a>function testFunction() {
<a name="line87"></a>  function f() {};
<a name="line88"></a>  assertEquals(f, goog.asserts.assertFunction(f));
<a name="line89"></a>  assertThrows(&#39;assertFunction(null)&#39;,
<a name="line90"></a>      goog.partial(goog.asserts.assertFunction, null));
<a name="line91"></a>  // Test error messages.
<a name="line92"></a>  doTestMessage(goog.partial(goog.asserts.assertFunction, null),
<a name="line93"></a>                &#39;Assertion failed: Expected function but got null: null.&#39;);
<a name="line94"></a>  doTestMessage(goog.partial(goog.asserts.assertFunction, 1234),
<a name="line95"></a>                &#39;Assertion failed: Expected function but got number: 1234.&#39;);
<a name="line96"></a>  doTestMessage(goog.partial(goog.asserts.assertFunction, null, &#39;ouch %s&#39;, 1),
<a name="line97"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testObject() {
<a name="line101"></a>  var o = {};
<a name="line102"></a>  assertEquals(o, goog.asserts.assertObject(o));
<a name="line103"></a>  assertThrows(&#39;assertObject(null)&#39;,
<a name="line104"></a>      goog.partial(goog.asserts.assertObject, null));
<a name="line105"></a>  // Test error messages.
<a name="line106"></a>  doTestMessage(goog.partial(goog.asserts.assertObject, null),
<a name="line107"></a>                &#39;Assertion failed: Expected object but got null: null.&#39;);
<a name="line108"></a>  doTestMessage(goog.partial(goog.asserts.assertObject, 1234),
<a name="line109"></a>                &#39;Assertion failed: Expected object but got number: 1234.&#39;);
<a name="line110"></a>  doTestMessage(goog.partial(goog.asserts.assertObject, null, &#39;ouch %s&#39;, 1),
<a name="line111"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>function testArray() {
<a name="line115"></a>  var a = [];
<a name="line116"></a>  assertEquals(a, goog.asserts.assertArray(a));
<a name="line117"></a>  assertThrows(&#39;assertArray({})&#39;,
<a name="line118"></a>      goog.partial(goog.asserts.assertArray, {}));
<a name="line119"></a>  // Test error messages.
<a name="line120"></a>  doTestMessage(goog.partial(goog.asserts.assertArray, null),
<a name="line121"></a>                &#39;Assertion failed: Expected array but got null: null.&#39;);
<a name="line122"></a>  doTestMessage(goog.partial(goog.asserts.assertArray, 1234),
<a name="line123"></a>                &#39;Assertion failed: Expected array but got number: 1234.&#39;);
<a name="line124"></a>  doTestMessage(goog.partial(goog.asserts.assertArray, null, &#39;ouch %s&#39;, 1),
<a name="line125"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>function testBoolean() {
<a name="line129"></a>  assertEquals(true, goog.asserts.assertBoolean(true));
<a name="line130"></a>  assertEquals(false, goog.asserts.assertBoolean(false));
<a name="line131"></a>  assertThrows(goog.partial(goog.asserts.assertBoolean, null));
<a name="line132"></a>  assertThrows(goog.partial(goog.asserts.assertBoolean, &#39;foo&#39;));
<a name="line133"></a>
<a name="line134"></a>  // Test error messages.
<a name="line135"></a>  doTestMessage(goog.partial(goog.asserts.assertBoolean, null),
<a name="line136"></a>                &#39;Assertion failed: Expected boolean but got null: null.&#39;);
<a name="line137"></a>  doTestMessage(goog.partial(goog.asserts.assertBoolean, 1234),
<a name="line138"></a>                &#39;Assertion failed: Expected boolean but got number: 1234.&#39;);
<a name="line139"></a>  doTestMessage(goog.partial(goog.asserts.assertBoolean, null, &#39;ouch %s&#39;, 1),
<a name="line140"></a>                &#39;Assertion failed: ouch 1&#39;);
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testElement() {
<a name="line144"></a>  assertThrows(goog.partial(goog.asserts.assertElement, null));
<a name="line145"></a>  assertThrows(goog.partial(goog.asserts.assertElement, &#39;foo&#39;));
<a name="line146"></a>  assertThrows(goog.partial(goog.asserts.assertElement,
<a name="line147"></a>      goog.dom.createTextNode(&#39;foo&#39;)));
<a name="line148"></a>  var elem = goog.dom.createElement(&#39;div&#39;);
<a name="line149"></a>  assertEquals(elem, goog.asserts.assertElement(elem));
<a name="line150"></a>}
<a name="line151"></a>
<a name="line152"></a>function testInstanceof() {
<a name="line153"></a>  /** @constructor */
<a name="line154"></a>  var F = function() {};
<a name="line155"></a>  goog.asserts.assertInstanceof(new F(), F);
<a name="line156"></a>  assertThrows(&#39;assertInstanceof({}, F)&#39;,
<a name="line157"></a>      goog.partial(goog.asserts.assertInstanceof, {}, F));
<a name="line158"></a>  // Test error messages.
<a name="line159"></a>  doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F),
<a name="line160"></a>                &#39;Assertion failed: instanceof check failed.&#39;);
<a name="line161"></a>  doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F, &#39;a %s&#39;, 1),
<a name="line162"></a>                &#39;Assertion failed: a 1&#39;);
<a name="line163"></a>
<a name="line164"></a>}
<a name="line165"></a>
<a name="line166"></a>function testObjectPrototypeIsIntact() {
<a name="line167"></a>  goog.asserts.assertObjectPrototypeIsIntact();
<a name="line168"></a>  var originalToString = Object.prototype.toString;
<a name="line169"></a>  Object.prototype.toString = goog.nullFunction;
<a name="line170"></a>  try {
<a name="line171"></a>    goog.asserts.assertObjectPrototypeIsIntact();
<a name="line172"></a>    Object.prototype.foo = 1;
<a name="line173"></a>    doTestMessage(goog.asserts.assertObjectPrototypeIsIntact,
<a name="line174"></a>        &#39;Failure: foo should not be enumerable in Object.prototype.&#39;);
<a name="line175"></a>  } finally {
<a name="line176"></a>    Object.prototype.toString = originalToString;
<a name="line177"></a>    delete Object.prototype.foo;
<a name="line178"></a>  }
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function testAssertionError() {
<a name="line182"></a>  var error = new goog.asserts.AssertionError(&#39;foo %s %s&#39;, [1, &#39;two&#39;]);
<a name="line183"></a>  assertEquals(&#39;Wrong message&#39;, &#39;foo 1 two&#39;, error.message);
<a name="line184"></a>  assertEquals(&#39;Wrong messagePattern&#39;, &#39;foo %s %s&#39;, error.messagePattern);
<a name="line185"></a>}
</pre>


</body>
</html>
